define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function ()
        {
            // 绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));

                if (panel.length > 0) 
                {
                    Controller.table[panel.attr("id")].call(this);

                    $(this).on('click', function (e) 
                    {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }

                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table:{
            back:function()
            {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        recyclebin_url: 'depot/back/recyclebin',
                        del_url: 'depot/back/destroy',
                        restore_url: 'depot/back/restore',
                        info_url: 'depot/back/info',
                        table: 'depot_back',
                    }
                });

                var BackTable = $("#BackTable");

                // 初始化表格
                BackTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.recyclebin_url,
                    pk: 'id',
                    sortName: 'id',
                    toolbar: '#BackToolbar',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                            {field: 'code', title: __('Code'), operate: 'LIKE'},
                            {field: 'ordercode', title: __('Storagecode'), operate: 'LIKE'},
                            {field: 'business.nickname', title: __('Busid')},
                            {field: 'contact', title: __('Contact'), operate: 'LIKE'},
                            {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                            {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'status', title: __('Status'), searchList: Config.backstatus, formatter: Table.api.formatter.status},
                            {field: 'deletetime', title: __('Deletetime'), sortable:true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: BackTable, 
                                events: Table.api.events.operate, 
                                formatter: Table.api.formatter.operate,
                                buttons:[
                                    {
                                        name: 'restore',
                                        title: '恢复',
                                        icon: 'fa fa-circle-o-notch',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax', 
                                        url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                        extend:"data-toggle='tooltip'",
                                        confirm: "是否确认恢复数据",
                                        success: () => BackTable.bootstrapTable('refresh')
                                    },
                                    {
                                        name:'info',
                                        title:'详情',
                                        classname:'btn btn-xs btn-success btn-dialog',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "80%"]\'', //重点是这一句
                                        url:$.fn.bootstrapTable.defaults.extend.info_url,
                                        icon:'fa fa-eye'
                                    },
                                ]
                            }
                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(BackTable);

                //给表格的按钮绑定点击事件
                $(".btn-restore").on('click', function(){
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(BackTable)

                    //弹框
                    layer.confirm(
                        '是否确认恢复数据', 
                        {title:"恢复提醒", btn: ['是', '否']},
                        function (index)
                        {
                            //发送ajax请求
                            Backend.api.ajax(
                                {url: $.fn.bootstrapTable.defaults.extend.restore_url+`?ids=${ids}`},
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    BackTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })
            },
            storage:function()
            {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        recyclebin_url: 'depot/storage/recyclebin',
                        del_url: 'depot/storage/destroy',
                        restore_url: 'depot/storage/restore',
                        info_url: 'depot/storage/info',
                        table: 'order',
                    }
                });

                var StorageTable = $("#StorageTable");

                // 初始化表格
                StorageTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.recyclebin_url,
                    pk: 'id',
                    sortName: 'id',
                    toolbar: '#StorageToolbar',
                    columns: [
                        [
                            {checkbox: true},
                            { field: 'id', title: __('Id'), operate:false },
                            { field: 'code', title: __('Code'), operate: 'LIKE' },
                            { field: 'supplier.name', title: __('supplier') },
                            { field: 'type', title: __('Type'), searchList: { "1": __('直销入库'), "2": __('退货入库') }, formatter: Table.api.formatter.normal },
                            { field: 'amount', title: __('StorageAmount'), operate: 'BETWEEN', sortable:true },
                            { field: 'status', title: __('Status'), searchList: Config.storagestatus, formatter: Table.api.formatter.status },
                            { field: 'createtime', title: __('Createtime'), operate: 'RANGE', sortable:true, addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                            {field: 'deletetime', title: __('Deletetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, sortable:true},
                            {
                                field: 'operate',
                                title: __('Operate'), 
                                table: StorageTable, 
                                events: Table.api.events.operate, 
                                formatter: Table.api.formatter.operate,
                                buttons:[
                                    {
                                        name: 'restore',
                                        title: '恢复',
                                        icon: 'fa fa-circle-o-notch',
                                        classname: 'btn btn-xs btn-success btn-magic btn-ajax', 
                                        url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                        extend:"data-toggle='tooltip'",
                                        confirm: "是否确认恢复数据",
                                        success: () => StorageTable.bootstrapTable('refresh')
                                    },
                                    {
                                        name: 'info',
                                        title: '入库详情',
                                        extend: 'data-toggle=\'tooltip\' data-area= \'["80%", "100%"]\'',
                                        classname: "btn btn-xs btn-success btn-dialog",
                                        icon: 'fa fa-eye',
                                        url: $.fn.bootstrapTable.defaults.extend.info_url,
                                    },
                                ]
                            }
                        ]
                    ]
                });

                // 为表格绑定事件
                Table.api.bindevent(StorageTable);

                //给表格的按钮绑定点击事件
                $(".btn-restore").on('click', function(){
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(StorageTable)

                    //弹框
                    layer.confirm(
                        '是否确认恢复数据', 
                        {title:"恢复提醒", btn: ['是', '否']},
                        function (index)
                        {
                            //发送ajax请求
                            Backend.api.ajax(
                                {url: $.fn.bootstrapTable.defaults.extend.restore_url+`?ids=${ids}`},
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    StorageTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })
            }
        },
        api: {
            bindevent: function () 
            {
                Form.api.bindevent($("form[role=form]"))
            },
        }
    }

    return Controller;
})